//
//  PopOverPresentationController.swift
//  weibo
//
//  Created by hm on 2020/4/27.
//  Copyright © 2020 hm. All rights reserved.
//

import UIKit

class PopOverPresentationController: UIPresentationController {
    
    //定义蒙版
    private lazy var coverView: UIView = UIView()
    
    override func containerViewWillLayoutSubviews() {
        super.containerViewWillLayoutSubviews()
        
        //设置弹出view的尺寸
//        presentedView?.frame = presentedFrame
        
        //添加蒙版
        setupCoverView()
    }
}

extension PopOverPresentationController {
    private func setupCoverView(){
        //默认有容器view
        //将自定义蒙版添加到容器view中最下面
        //避免蒙版将弹出PopView盖住
        containerView?.insertSubview(coverView, at: 0)
            
        //设置蒙版view的背景色为透明灰色
        coverView.backgroundColor = UIColor(white: 0.8, alpha: 0.2)
        //设置蒙版view的尺寸为全屏
        coverView.frame = containerView!.bounds
        
        //添加蒙版view的手势
        let tapGes = UITapGestureRecognizer(target: self, action: #selector(self.coverViewClick))
        coverView.addGestureRecognizer(tapGes)
    }
}

extension PopOverPresentationController{
    @objc private func coverViewClick(){
        //点击蒙版时，关闭弹出PopView
        presentingViewController.dismiss(animated: true, completion: nil)
    }
}
